import Dialog from '../../../../dist/dialog/dialog';
const app = getApp();
Component({
  options: {
    styleIsolation: 'shared' 
  },
  properties: {
    meal_show: {
      type: Boolean,
      value: false
    },
    type: {
      type: String,
      value: ''
    },
    currentPackage: {
      type: null,
      value: ''
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    mealList: [{
      id:1,
      name: '按单次单条信息费用',
      tip: '单次单条信息费用10元/条'
    },{
      id:2,
      name: '按点击量收费',
      tip: '1次点击收费10元，余额需满11元/条'
    },{
      id:3,
      name: '按季度信息数量收费套餐',
      tip: '季度信息数量999，收费888元/条'
    },{
      id:4,
      name: '按年度信息数量收费套餐',
      tip: '单次单条信息费用10元/条'
    }],
    package_value: "",
    package_list: []
  },
  observers: {
    "meal_show":  function(value) {
      if(value) {
        this.setData({
          package_value: this.data.currentPackage && this.data.currentPackage.other_data ? this.data.currentPackage.other_data.value : this.data.currentPackage && this.data.currentPackage.value ? this.data.currentPackage.value : ""
        })
        this.getBuyPackage();
      }
    },
  },
  lifetimes: {
    attached() {
    }
  },
  methods: {
    btnCancal() {
      if(this.data.currentPackage || this.data.type == "change") {
        this.setData({
          meal_show: false
        })
      }else {
        this.surePackage();
      }
    },
    onChange(event) {
      console.log(event,'678');
      this.setData({
        package_value: event.detail,
      });
    },
    getBuyPackage() {
      let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.buy-package.index");
      app._postNetWork({
        url: baseUrl,
        data: {  },
        success: ({data : { data,result,msg }}) => {
          console.log(8888888,data,result,msg);
          if (result !== 1) {
            app.tips(msg)
            return false;
          }
          this.setData({
            package_list: data.package_list,
            package_value: this.data.package_value ? this.data.package_value : data.package_list.length > 0 ? data.package_list[0].value : ""
          })
        }
      });
    },
    surePackage() {
      Dialog.confirm({
        title: '提示',
        message: '更换套餐后，之前的套餐将失效，是否确认更换?',
      })
        .then(() => {
          let packageArr = this.data.package_list.filter(item => item.value == this.data.package_value);
          let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.buy-package.change");
          app._postNetWork({
            url: baseUrl,
            data: { package_type: packageArr[0].value },
            success: ({data : { data,result,msg }}) => {
              if (result !== 1) {
                app.tips(msg)
                return false;
              }
              if(data.need_pay == 1) {
                wx.navigateTo({
                  url: '/packageD/buy/orderPay/orderPay?order_id=' + data.order_id + '&status=2'
                });
              }else {
                app.tips(msg)
                this.triggerEvent('optionPackage',packageArr);
              }
            }
          });
        })
        .catch(() => {
        });
    }
  }

})